<template>
  <div>
    <p style="text-align:right">
      <el-button type="primary"  @click="$router.push({path: '/goods/listOfAllGoods'})">返回商品列表</el-button>
    </p>
    <!-- 基本信息编辑 -->
    <goods-base-info-setting
      type='edit'
      :categoryId = 'goodsData.categoryId'
      :title = 'goodsData.title'
      :sellPoint = 'goodsData.sellPoint'
      :labelList = 'goodsData.labelList'
      :factory = 'goodsData.factory'
      ref="goodsBaseInfoSetting"
    ></goods-base-info-setting>

    <!-- 商品sku编辑 -->
    <goods-sku-setting
      type='edit'
      :categoryid = 'goodsData.categoryId'
      :skuListData = 'goodsData.skuList'
      ref='goodsSkuSetting'
    ></goods-sku-setting>

    <!-- 首页占位图、列表占位图、商品轮播图、商品详情图编辑 -->
    <goods-pic-upload-for-index
      type='edit'
      ref='goodsPicUploadForIndex'
      :recommendImage = 'goodsData.recommendImage'
      :listImage = 'goodsData.listImage'
      :switchImageList = 'goodsData.switchImageList'
      :detailImageList = 'goodsData.detailImageList'
    ></goods-pic-upload-for-index>

    <!-- 物流信息编辑 -->
    <goods-logistics-setting
      type='edit'
      ref='goodsLogisticsSetting'
      :providerId = 'goodsData.providerId'
      :channelId = 'goodsData.channelId'
      :returnContacts = 'goodsData.returnContacts'
      :returnContactInfo = 'goodsData.returnContactInfo'
      :returnAddress = 'goodsData.returnAddress'
      :freight = '0'
    ></goods-logistics-setting>

    <el-button type="primary" @click="onSubmit">提交</el-button>
    <el-button>取消</el-button>
  </div>
</template>
<script>
  import {
    getProduct,
    saveGoodsInfo
  } from 'src/api/index.js'
  import goodsSkuSetting from 'components/goods/goodsSkuSetting.vue'
  import goodsPicUploadForIndex from 'components/goods/goodsPicUploadForIndex.vue'
  import goodsBaseInfoSetting from 'components/goods/goodsBaseInfoSetting.vue'
  import goodsLogisticsSetting from 'components/goods/goodsLogisticsSetting.vue'
  export default {
    beforeRouteEnter (to, from, next) {
      let productId = to.query.productId
      getProduct({
        productId
      }).then((res) => {
        let result = res.data
        if (result.code === 100) {
          next(vm => {
            vm.goodsData = result.data
          })
        } else {
          next()
        }
      })
    },
    created () {
      // 单独设置面包屑
      this.$route.meta.breadcrumbData = [
        {
          butRule: '/goods',
          name: '商品管理'
        },
        {
          butRule: '/goods/editGoods',
          name: '商品编辑'
        }
      ]
    },
    data () {
      return {
        goodsData: {}
      }
    },
    components: {
      goodsSkuSetting,
      goodsPicUploadForIndex,
      goodsBaseInfoSetting,
      goodsLogisticsSetting
    },
    methods: {
      formValidation () {
        // 各表单校验
        let baseInfoValidation = this.$refs.goodsBaseInfoSetting.formValidation
        let skuSettingValidation = this.$refs.goodsSkuSetting.formValidation
        let logisticsSettingValidation = this.$refs.goodsLogisticsSetting.formValidation
        let picUploadValidation = this.$refs.goodsPicUploadForIndex.formValidation
        let condition =
          baseInfoValidation() &&
          skuSettingValidation() &&
          logisticsSettingValidation() &&
          picUploadValidation()
        if (condition) {
          return 1
        }
        return 0
      },
      onSubmit () {
        let that = this
        let result = this.formValidation()
        if (result) {
          let productId = this.$route.query.productId
          let param = {
            ...this.$refs.goodsBaseInfoSetting.getFormData(),
            ...this.$refs.goodsSkuSetting.getFormData(),
            ...this.$refs.goodsLogisticsSetting.getFormData(),
            ...this.$refs.goodsPicUploadForIndex.getFormData(),
            productId
          }
          let loadding = this.$loading()
          saveGoodsInfo(param).then((res) => {
            loadding.close()
            let result = res.data
            if (result.code === 100) {
              this.$alert('编辑成功', '提示', {
                callback () {
                  that.$router.push({
                    path: '/goods/listOfAllGoods'
                  })
                }
              })
            } else {
              this.$message.error(result.message)
            }
          })
        }
      },
      setCategoryId (id) {
        this.categoryId = id
      }
    }
  }
</script>
